﻿/*!
 * Copyright (c), 上海沃恩信息科技有限公司.
 * All rights reserved.
 * Licensed under BSD (https://www.pittypat.work/bsd.html).
 */

using System.Net;
using System.Threading.Tasks;

namespace Pittypat.Web
{
    /// <summary>
    /// 处理与用户通知相关的请求。
    /// </summary>
    static class NotificationHttpHandler
    {
        /// <summary>
        /// 异步处理指定的请求。
        /// </summary>
        /// <param name="request">要处理的请求。</param>
        /// <returns>结果消息。</returns>
        internal static async Task<Message> ProcessAsync(Request request)
        {
            var notificationService = NotificationService.Current;

            if (notificationService == null)
            {
                return Message.Create("没有提供有效的通知中心服务，请检查配置文件的 notificationService 配置项目。\r\n这是一个配置错误，需要系统管理人员调整配置文件。");
            }

            var path = request.LowerAbsPath;

            if (path == "/notification/count")
            {
                return await ProcessCountAsync(request, notificationService);
            }
            else if (path == "/notification/list")
            {
                return await ProcessListAsync(request, notificationService);
            }
            else if (path == "/notification/read")
            {
                return await ProcessReadAsync(request, notificationService);
            }

            // 其他不受支持的请求路径
            return Message.Create(HttpStatusCode.NotFound);
        }

        /// <summary>
        /// 处理查询新通知数目的请求。
        /// </summary>
        /// <param name="request">当前请求。</param>
        /// <param name="notificationService">当前通知服务。</param>
        /// <returns>要发送给客户端的响应消息。</returns>
        static async Task<Message> ProcessCountAsync(Request request, INotificationService notificationService)
        {
            var userId = request.User.Id;

            if (userId.HasValue)
            {
                var count = await notificationService.CountAsync(userId.Value, request.Progress);

                return Message.Create(count);
            }
            else
            {
                return Message.Create("用户未登录或会话已经过期，没有权限访问 /notification/count/。\r\n请重新登录。");
            }
        }

        /// <summary>
        /// 处理查询新通知列表的请求。
        /// </summary>
        /// <param name="request">当前请求。</param>
        /// <param name="notificationService">当前通知服务。</param>
        /// <returns>要发送给客户端的响应消息。</returns>
        static async Task<Message> ProcessListAsync(Request request, INotificationService notificationService)
        {
            var userId = request.User.Id;

            if (userId.HasValue)
            {
                var list = await notificationService.ListAsync(userId.Value, request.Progress) ?? NotificationList.Empty;

                return Message.Create(list);
            }
            else
            {
                return Message.Create("用户未登录或会话已经过期，没有权限访问 /notification/list/。\r\n请重新登录。");
            }
        }

        /// <summary>
        /// 处理将通知标记为已读的请求。
        /// </summary>
        /// <param name="request">当前请求。</param>
        /// <param name="notificationService">当前通知服务。</param>
        /// <returns>要发送给客户端的响应消息。</returns>
        static async Task<Message> ProcessReadAsync(Request request, INotificationService notificationService)
        {
            // 当前用户的 Id
            var userId = request.User.Id;

            if (userId.HasValue)
            {
                // 目标通知的 Id
                string sid = request.Form["id"];

                // 将通知 Id 转换为 uuid 类型
                var notificationId = @uuid.Parse(sid);

                if (!notificationId.HasValue)
                {
                    return Message.Create("没有提供要标记为已读的消息的 Id。\r\n这是一个编码错误，需要开发人员提交必要的参数。");
                }

                // 返回标记时间
                var readTime = await notificationService.ReadAsync(userId.Value, notificationId.Value, request.Progress);

                return Message.Create(readTime);
            }
            else
            {
                return Message.Create("用户未登录或会话已经过期，没有权限访问 /notification/read/。\r\n请重新登录。");
            }
        }
    }
}
